﻿using Asxsyd92Core.Utils.Provider;
using System.Collections.Generic;
using System.Linq;
using WebOS.Dal;
using WebOS.Modle;

namespace WebOS.Bll
{
    public class UsersBll
    {
        public static UsersBll Instance
        {
            get { return SingletonProvider<UsersBll>.Instance; }
        }


        public List<Users> GetUsersList(string title, string type, int page, int limit)
        {

            var da = UsersDal.Instance.GetList("SELECT u.ID,u.Account,u.Name,o.Name as Organize,o.ID as OrganizeID,o.Leader from  Users u,Organize o,UsersRelation ur where o.ID=ur.OrganizeID and ur.UserID=u.ID and o.Status=0", null).ToList();
            var ks = da.GroupBy(c => c.Organize);

            return da;

        }
        public IEnumerable<object> GetUsers(string title, string desc, int page, int limit)
        {
            IEnumerable<object> o = null;
            if (title != null && title != "")
            {
                string sql = "";
                string counts = "(select count(1) from Users t,Role r where t.identifier=r.identifier ";
                string sqls = "", f = "";
                title = title.Replace("'", "''");
                title = "'%" + title + "%'";
                sql = sql + " and Name like " + title;
                counts = counts + " and t.Name like " + title;
                f = f + " and t.Name like " + title;

                //降序
                sql = sql + "and RowNumber > " + limit + " * (" + page + " - 1)";
                //分页
                var fy = "(SELECT ROW_NUMBER() OVER(ORDER BY t." + desc + " DESC) AS RowNumber,t.*,r.Name  as RoleName FROM Users t,Role r where  t.identifier=r.identifier " + f + ")";
                sqls = "select top " + limit + "  *," + counts + ") as Count  from " + fy + " as A where 1=1 " + sql;

                o = Asxsyd92Core.Utils.Data.SQLServer.SqlFromData.GetList(sqls).ToList();
                return o;
            }
            else
            {
                string sql = "";

                string counts = "(select count(1) from Users t,Role r where t.identifier=r.identifier ";
                string sqls = "", f = "";
                //降序
                sql = sql + "and RowNumber > " + limit + " * (" + page + " - 1)";
                //分页
                var fy = "(SELECT ROW_NUMBER() OVER(ORDER BY t." + desc + " DESC) AS RowNumber,t.*,r.Name  as RoleName FROM Users t,Role r where  t.identifier=r.identifier " + f + ")";
                sqls = "select top " + limit + "  *," + counts + ") as Count  from " + fy + " as A where 1=1 " + sql;


                o = Asxsyd92Core.Utils.Data.SQLServer.SqlFromData.GetList(sqls).ToList();
                return o;
            }



        }


        public Users Login(string user)
        {
            var da = UsersDal.Instance.GetList("SELECT u.ID,u.Account,u.avatar,u.Status,u.Name,o.Name as Organize,u.identifier,o.ID as OrganizeID,o.Leader ,u.Password from  Users u,Organize o,UsersRelation ur where o.ID=ur.OrganizeID and ur.ID=u.ID and o.Status=0 and u.Account='" + user.ToLower() + "'", null).FirstOrDefault();
            return da;
        }


    }

}
